package com.neudu.ssrs_yyzxservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.neudu.ssrs_yyzxservice.common.JSONReturn;
import com.neudu.ssrs_yyzxservice.pojo.Nurselevel;
import com.neudu.ssrs_yyzxservice.service.NurselevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class NurselevelController {

    @Autowired
    private NurselevelService nurselevelService;
    @Autowired
    private JSONReturn jsonReturn;

    // 获取所有护理等级信息
    @PostMapping("/get_all_levels")
    public String getAllLevels() {
        try {
            List<Nurselevel> levels = nurselevelService.list();
            return jsonReturn.returnSuccess(levels);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据护理级别名称模糊查询
    @PostMapping("/get_levels_by_name")
    public String getLevelsByName(@RequestBody Nurselevel nurselevel) {
        try {
            LambdaQueryWrapper<Nurselevel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(ObjectUtils.isEmpty(nurselevel.getLevelName()), Nurselevel::getLevelName, nurselevel.getLevelName());
            List<Nurselevel> levels = nurselevelService.list(queryWrapper);
            return jsonReturn.returnSuccess(levels);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据ID获取护理等级信息
    @PostMapping("/get_level_by_id")
    public String getLevelById(@RequestBody Nurselevel nurselevel) {
        try {
            Nurselevel level = nurselevelService.getById(nurselevel.getId());
            return jsonReturn.returnSuccess(level);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 添加护理等级
    @PostMapping("/add_level")
    public String addLevel(@RequestBody Nurselevel nurselevel) {
        try {
            nurselevelService.save(nurselevel);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 修改护理等级
    @PostMapping("/modify_level")
    public String modifyLevel(@RequestBody Nurselevel nurselevel) {
        try {
            nurselevelService.updateById(nurselevel);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 删除护理等级
    @PostMapping("/remove_level")
    public String removeLevel(@RequestBody Nurselevel nurselevel) {
        try {
            nurselevelService.removeById(nurselevel.getId());
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}